package ru.sanluck.books;

import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ru.sanluck.lib.BookReader;
import ru.sanluck.lib.BookReaderEntity;
import ru.sanluck.lib.BookReaderRepository;

@Service
@AllArgsConstructor
public class DefaultBookReadersService implements BookReadersService {

    private final BookReaderRepository bookReaderRepository;
    private final BookReaderMapper bookReaderMapper;

    @Transactional
    @Override
    public void registerInLibrary(String tgNick, String firstName, String secondName) {
        BookReaderEntity reader = new BookReaderEntity();
        reader.setFirstName(firstName);
        reader.setSecondName(secondName);
        reader.setTgNick(tgNick);
        bookReaderRepository.save(reader);
    }

    @Override
    public BookReader getBookReader(String tgNick) {
        BookReaderEntity reader = bookReaderRepository.findByTgNick(tgNick);
        return bookReaderMapper.toDomain(reader);
    }
}
